package com.ozy.kxreader.mvp.presenter

import com.ozy.kxreader.app.RxLifecycleUtils
import com.ozy.kxreader.base.BasePresenter
import com.ozy.kxreader.di.scope.ActivityScope
import com.ozy.kxreader.mvp.contract.BookListContract
import com.ozy.kxreader.toast
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

/**
 * Created by 欧源 on 2018/1/31.
 */
@ActivityScope
class BookListPresenter
@Inject constructor(model: BookListContract.Model, view: BookListContract.View)
    : BasePresenter<BookListContract.Model, BookListContract.View>(model, view) {


    private var mStart: Int = 0

    private var mLimit: Int = 30


    fun getBookList(gender: String?, major: String?, minor: String?, type: String?, isRefresh: Boolean) {

        if (isRefresh) mStart = 0

        mModel.getBookList(gender, major, minor, type, mStart, mLimit)
                .subscribeOn(Schedulers.io())
                .retry()
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe({ res ->
                    var books = res.books
                    mStart += books?.size!!
                    mRootView?.showBookList(books,isRefresh)

                }, {

                })

    }


}